<?php

class TournamentController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
    	$this->view->title = 'Events';
		$this->view->headTitle($this->view->title, 'PREPEND');
		if($input = $this->_checkInput('id')){
			if($input->id){
				$t = new Application_Model_Tournament($input->id);
		    	$events = $t->getEvents(false);
		    	$this->view->title = $t->title;		    	
			}
			else{
				$this->view->title = 'All';
				$e = new Application_Model_Event();
				$rowset = $e->getAll(false, null, false);
				foreach ($rowset as $row){
					$events[] = new Application_Model_Event($row->id);
				}
			}
			$this->view->events = $events;
	    }
	    else{
	    	throw new Zend_Controller_Action_Exception('Invalid input');
	    }
		
    }
	private function _checkInput($param, $field = null){
		$validatorsSet = array();
		$validatorsSet[] = 'Int';
		$validatorsSet[] = new Zend_Validate_GreaterThan(-1);
		//$validatorsSet[] = new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::ALL);
		if($field){
			$validatorsSet[] = new Zend_Validate_Db_RecordExists(array('table' => $field, 'field' => $param));
		}
    	$validators = array($param => $validatorsSet);
	    $input = new Zend_Filter_Input(null, $validators);
	    $input->setData($this->getRequest()->getParams());
	    if($input->isValid()){
	    	return $input;
	    }
	    else{
	    	return false;
	    }		
	}
}